<!--
 * @Descripttion: 
 * @version: 
 * @Author: wtzhang
 * @Date: 2021-05-20 09:46:05
 * @LastEditors: wtzhang
 * @LastEditTime: 2021-05-28 09:05:13
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // let oReq = new XMLHttpRequest()
    // // 当请求记载成功之后要调用指定的函数
    // oReq.onload = function () {
    //   console.log(oReq.responseText)
    // }
    // oReq.open('get', './data/test.json', true)
    // oReq.send()

    // 封装
    function get(url, callback) {
      let oReq = new XMLHttpRequest()
      // 当请求记载成功之后要调用指定的函数
      oReq.onload = function () {
        // console.log(oReq.responseText)
        callback(oReq.responseText) // 执行回调
      }
      oReq.open('get', url, true)
      oReq.send()
    }
    get('./data/test.json', function(data) {
      console.log(data);
    })
  </script>
</body>

</html>